package ws.very.util.lang
import java.util.concurrent.Executors
import java.util.concurrent.Callable

object Threads {
  val pool = Executors.newCachedThreadPool
  Runtime.getRuntime().addShutdownHook(new Thread {
    override def run = {

      pool.shutdown
      super.run
    }
  })

  def apply[R](fn: => R) =
    pool submit new Callable[R] {
      def call = fn
    }

  def aplly(fn: => Unit) =
    pool execute new Runnable {
      def run = fn
    }
}

trait ThreadConverter {
  implicit def makeRunnable(f: => Unit): Runnable = new Runnable() { def run() = f }

  implicit def makeCallable[T](f: => T): Callable[T] = new Callable[T]() { def call() = f }

  implicit def makeThread(f: => Unit) = new Thread {
    override def run = f
  }
}
